<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        outline: none;
    }

    .name,
    .sex,
    .grade {
        width: 200px;
        height: 30px;
        background: skyblue;
        border: none;
        margin-left: 88px;
        margin-right: 50px;
    }

    .tj {
        width: 100px;
        height: 30px;
        background: pink;
        border: none;
    }

    h1 {
        text-align: center;
    }
</style>

<body>
    <input type="text" class="name" value="" placeholder="请输入学生姓名">
    <input type="text" class="sex" value="" placeholder="请输入学生性别">
    <input type="text" class="grade" value="" placeholder="请输入学生成绩">
    <input type="button" value="一键添加" class="tj">
    <table class="table table-bordered " id="tableSort">
        <h1>学生成绩表</h1>
        <thead>
            <tr>
                <th>姓名</th>
                <th>性别</th>
                <th>成绩</th>
            </tr>
        </thead>
        <tbody>
            <!-- <tr>
                <td>Tanmay</td>
                <td>Bangalore</td>
                <td>560001</td>
            </tr> -->
        </tbody>
    </table>
    <button class="mark">一键排序</button>
</body>
<script>
    let name_ = document.querySelector(".name");
    let sex_ = document.querySelector(".sex");
    let grade_ = document.querySelector(".grade");
    let tj = document.querySelector(".tj");
    let tbody = document.querySelector("tbody");
    tj.onclick = function () {
        if (name_.value) {
            tbody.innerHTML += `
        <tr>
                <td>${name_.value}</td>
                <td>${sex_.value}</td>
                <td>${grade_.value}</td>
            </tr>
        `
        }
    }
    let mark = document.querySelector('.mark');  // 获取成绩元素
    let num = 1;
    mark.onclick =  () =>{                                                 //点击成绩单元格，进行排序
        let arr = [];
        for (let i = 0, len = tbody.rows.length; i < len; i++) {                //将每一行的数据存放进数组arr中
            arr.push(tbody.rows[i]);
        }
        arr.sort(function (a, b) {                                            //进行成绩排序
            let sort = num * (a.cells[2].innerHTML - b.cells[2].innerHTML);
            return sort;
        });
        num = -num;
        for (let i = 0, len = tbody.rows.length; i < len; i++) {              //排序后，重新添加进表格
            tbody.appendChild(arr[i]);
        }
    }
</script>

</html>